//
//  UNNavigationController.swift
//  Unibet
//
//  Created by HaoSheng on 2020/4/15.
//  Copyright © 2020 蜗牛. All rights reserved.
//

import UIKit
///自定义导航栏
open class UNNavigationController: UINavigationController {
    
    public var statusView: UIView = UIView.init()
    
    public enum NavBarBackgroundColor {
        ///白色
        case white
        ///灰色 F6F6FB
        case grey
        ///黑色 1F1E1E
        case black
        ///红色 FB3359
        case red
        ///淡红色
        case pink
        ///蓝色
        case bule
    }
    
    ///设置背景相关颜色
    public var backgroundColor: NavBarBackgroundColor = .white{
        didSet{
            switch backgroundColor {
            case .white:
                //标题颜色
                self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black];
                navigationBackgroundColor(color:.white)
                break;
            case .grey:
                self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black];
                navigationBackgroundColor(color:.FromHex(rgb: 0xF6F6FB))
                break;
                
            case .black:
                self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white];
                navigationBackgroundColor(color:.FromHex(rgb: 0x1F1E1E))
                break;
                
            case .red:
                self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white];
                navigationBackgroundColor(color:.FromHex(rgb: 0xFB3359))
                break
            case .pink:
                self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white];
                navigationBackgroundColor(color:.FromHex(rgb: 0xFF4848))
                break
            case .bule:
                self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white];
                navigationBackgroundColor(color: .blue)
                break
            }
        }
    }
    
    
    ///导航栏背景色
    public func navigationBackgroundColor(color:UIColor){
        self.navigationBar.barTintColor = color
        if #available(iOS 15.0, *) {
            let app = navigationBar.standardAppearance
            app.configureWithOpaqueBackground()
            app.backgroundColor = color
            
//            app.shadowImage = MyTool.image(from: .FromHex(rgb: 0x000000, alpha: 0))//隐藏下方线条
            app.shadowImage = UIColor.FromHex(rgb: 0x000000, alpha: 0).image()
            
            
            navigationBar.standardAppearance = app
            navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
        }
    }
    
    ///设置状态栏背景色
    public func setStatusBackgroundColor(color: UIColor){
        let statusGreen = UIView.init(frame: .init(x: 0, y: -StatusHeight, width: MainWidth, height: StatusHeight))
        statusGreen.backgroundColor = color
        self.navigationBar.addSubview(statusGreen)
    }
    
    
    ///隐藏下方的分割线
    public var hidLine: Bool{
        set{
            if newValue {
                self.navigationBar.setBackgroundImage(UIColor.FromHex(rgb: 0x000000, alpha: 0).image(), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)
                self.navigationBar.shadowImage = UIImage()
            }else{
                self.navigationBar.setBackgroundImage(nil, for: .default)
                self.navigationBar.shadowImage = nil
            }
        }
        get{return false}
    }
    
    
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        
        self.interactivePopGestureRecognizer!.delegate = self;//右滑返回手势代理
        
        self.navigationBar.isTranslucent = false;//去除半透明效果
        
        
        let appearance = UINavigationBar.appearance()
        appearance.shadowImage = UIImage()
        appearance.layer.shadowColor = UIColor.FromHex(rgb: 0xC9C9C9).cgColor
        appearance.layer.shadowRadius = 20
        appearance.layer.shadowOpacity = 0.2
        appearance.layer.shadowOffset = CGSize(width: 0, height: 5)
//        appearance.tintColor = UIColor.primaryTextColor //前景色，按钮颜色
        appearance.isTranslucent = false // 导航条背景是否透明
        appearance.barTintColor = .white //背景色，导航条背景色
        appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .medium)] // 设置导航条标题颜色，还可以设置其它文字属性，只需要在里面添加对应的属性
        
        // 解决iOS15 barTintColor设置无效的问题，参考https://developer.apple.com/forums/thread/682420
        if #available(iOS 15.0, *) {
            let newAppearance = UINavigationBarAppearance()
            newAppearance.configureWithOpaqueBackground()
            newAppearance.backgroundColor = .white
            newAppearance.shadowImage = UIColor.FromHex(rgb: 0x000000, alpha: 0).image()//隐藏下方线条
            newAppearance.shadowColor = nil
            newAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18*KWIDTH, weight: .medium)]
            
            appearance.standardAppearance = newAppearance
            appearance.scrollEdgeAppearance = appearance.standardAppearance
        }
        
        
        
        
    }
    
    ///重写状态栏颜色
    public override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent;
    }
    
    /// 重写此方法让 preferredStatusBarStyle 响应
    public override var childForStatusBarStyle: UIViewController? {
        return self.topViewController
    }
    
    
    
    //重写导航栏的push方法，因为需要自定义返回按钮
    open override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if self.viewControllers.count > 0 {
            
            let imgStr = backgroundColor == .white || backgroundColor == .grey ? "back_black" : "back_white"
            
            //隐藏除了第一个界面下方的TabBar
            viewController.hidesBottomBarWhenPushed = true;
            viewController.navigationItem.leftBarButtonItem = UNBarButtonItem.itemWithTitleAndImage(title: "      ", titleFont: KFONT(font: 14), titleColor: .blue, imageString: imgStr, leftTitleRightImage: nil, target: self, action: #selector(back))
        }
        
        super.pushViewController(viewController, animated: animated);
    }
    
    @objc public func back(){
        if topViewController?.responds(to: #selector(backAction)) != nil {
            topViewController!.backAction()
        }
    }
    
    
    
}
//MARK:- UIGestureRecognizerDelegate手势滑动代理

//extension UINavigationController: UIGestureRecognizerDelegate{
//
//    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
//
//        return self.viewControllers.count > 1
//
//    }
//}
